package com.handpay.framework;

import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.bean.ActionBar;
import com.handpay.zztong.hp.log.HPLog;
import com.handpay.zztong.hp.utils.BitmapUtil;

/**
 * Created by sxshi on 2016/5/9.
 */
public class KxbActivity extends ZZTongActivity {
    private ImageView iv_scanner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_kxb);
        super.onCreate(savedInstanceState);
        initView();
        initListener();
    }

    /***
     * 初始化监听
     */
    private void initListener() {
        iv_scanner.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                iv_scanner.setDrawingCacheEnabled(true);
                Bitmap cache = iv_scanner.getDrawingCache();
                if (cache!=null){
                    HPLog.d("tag","获取Bitmap成功");
                    new SaveImageTask().execute(cache);
                }else {
                    HPLog.d("tag","获取Bitmap失败");
                }
                return true;
            }
        });
    }

    /***
     * 初始化控件
     */
    private void initView() {
        iv_scanner = (ImageView) findViewById(R.id.iv_scanner);
    }

    @Override
    public ActionBar getActivityActionBar() {
        return new ActionBar(this.getString(R.string.kxb), true);
    }

    /***
     * 异步保存二维码图片
     */
    private class SaveImageTask extends AsyncTask<Bitmap,Void,String>{
        @Override
        protected String doInBackground(Bitmap... params) {
            byte[] bitmap2Bytes = BitmapUtil.Bitmap2Bytes(params[0]);
            if (bitmap2Bytes.length>0){
               HPLog.d("tag","压缩成功");
            }
           String path= BitmapUtil.saveByte_File(bitmap2Bytes,"kaxiaobao",KxbActivity.this);
            return path;
        }
        @Override
        protected void onPostExecute(String path) {
            if (!TextUtils.isEmpty(path)){
                Toast.makeText(KxbActivity.this,getResources().getString(R.string.save_pic_success),Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(KxbActivity.this,getResources().getString(R.string.save_pic_fail),Toast.LENGTH_SHORT).show();
            }
            iv_scanner.setDrawingCacheEnabled(false);
        }
    }
}
